import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
import { StudentService } from './student.service';
import { StudentInput } from './dto/student-input.type';
// import { Query } from '@nestjs/common';
import { StudentResult, StudentResults, StudentType } from './dto/student.type';
import { GqlAuthGuard } from '@/common/guards/auth.guards';
import { UseGuards } from '@nestjs/common';
// import { Result } from '@/common/dto/result.type';
import {
  STUDENT_NOT_EXIST,
  SUCCESS,
  UPDATE_ERROR,
} from '@/common/constants/code';
import { CurUserId } from '@/common/decorators/current-user.decorator';
import { PageInput } from '@/common/dto/page.input';

@Resolver(() => StudentType)
@UseGuards(GqlAuthGuard) //启用守卫
export class StudentResolver {
  constructor(private readonly studentService: StudentService) {}

  @Query(() => StudentResult, { description: '使用ID查询用户' })
  async getStudentInfo(@CurUserId() userId: string): Promise<StudentResult> {
    // const id = cxt.req?.user?.id || '';
    const student = await this.studentService.findById(userId);
    if (student) {
      return {
        code: SUCCESS,
        data: student,
        message: '获取成功',
      };
    }
    return {
      code: STUDENT_NOT_EXIST,
      message: '学生不存在',
    };
  }
  @Mutation(() => StudentResult)
  async commitStudentInfo(
    @Args('params') params: StudentInput,
    @CurUserId() userId: string,
  ): Promise<StudentResult> {
    // const id = cxt.req?.user?.id || '';
    const id = userId;
    const student = await this.studentService.findById(id);
    if (student) {
      const res = await this.studentService.updateById(id, params);
      if (res) {
        return {
          code: SUCCESS,
          message: '更新成功',
        };
      }
    }
    return {
      code: UPDATE_ERROR,
      message: '更新失败',
    };
  }
  @Query(() => StudentResults)
  async getStudents(@Args('page') page: PageInput): Promise<StudentResults> {
    const { pageNum, pageSize } = page;
    const [results, total] = await this.studentService.findStudents({
      start: (pageNum - 1) * pageSize,
      size: pageSize,
    });

    return {
      code: SUCCESS,
      data: results,
      page: {
        pageNum,
        pageSize,
        total,
      },
      message: '获取成功',
    };

  }
  @Mutation(() => Boolean, { description: '新增用户' })
  async createStudent(@Args('params') params: StudentInput): Promise<boolean> {
    return await this.studentService.create(params);
  }
  //   @Query(() => StudentType, { description: '使用ID查询用户' })
  //   async find(@Args('id') id: string): Promise<StudentType> {
  //     return await this.studentService.findById(id);
  //   }
  //   @Mutation(() => Boolean, { description: '更新用户' })
  //   async update(
  //     @Args('id') id: string,
  //     @Args('params') params: StudentInput,
  //   ): Promise<boolean> {
  //     return await this.studentService.update(id, params);
  //   }
  //   @Mutation(() => Boolean, { description: '删除用户' })
  //   async del(@Args('id') id: string): Promise<boolean> {
  //     return await this.studentService.del(id);
  //   }
}
